package com.txby.platformApi.component.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gexin.fastjson.JSON;
import com.jck.mybatis.util.PageList;
import com.jck.tools.ChkUtil;
import com.txby.pf.common.annotation.SysLog;
import com.txby.pf.common.utils.PageUtils;
import com.txby.pf.common.utils.Query;
import com.txby.pf.common.utils.R;
import com.txby.pf.common.validator.ValidatorUtils;
import com.txby.pf.common.validator.group.AddGroup;
import com.txby.platformCore.entity.SysOption;

/**
 * 问题反馈
 */
@RestController
@RequestMapping("/sys/count")
public class CountController  extends CommonController {
	/**
	 * 问题反馈
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:count:list")
	public R list(@RequestParam Map<String, Object> params){
		String sql="";
		Integer optionId=null;//optionId
		Map paramMap = new HashMap<>();
		List optionIdList = queryOptionIdList();//所有题干ID
		Map mapi = queryList();
		List list = new ArrayList();
		List mapList = new ArrayList();
		String optionIdIn ="";
		/*for (int i = 0; i < optionIdList.size(); i++) {
			mapi = (Map) optionIdList.get(i);//题干ID
			optionId = new Integer(mapi.get("id_").toString());
			list.add(optionId);
		}
		list.toString().replace("[", "(").replace("]", ")");
		System.out.println(list);*/
			/*optionId = 1;
			 sql = "SELECT type FROM t_txby_bd_sysOption t1, t_txby_bd_sysCusTopic t2 WHERE t1.topicId = t2.id_ AND t1.id_ = "+optionId;
			Map map =(Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
			String topic_type = map.get("type").toString();
			paramMap.put("optionId",  optionId);
			if (topic_type.equals("1")||topic_type.equals("2")) {
				 sql = "select DISTINCT t2.value as name, (((SELECT count(*) FROM `t_txby_bd_sysAnswer` where optionId = #{pojo.optionId})*100)/ (SELECT count(*) FROM `t_txby_bd_sysAnswer`"
				 		+ " where topicId = "+topic_type+")) as value from t_txby_bd_sysAnswer t1,t_txby_bd_sysOption t2 where t1.optionId = t2.id_ and t1.optionId = #{pojo.optionId}";
			}else if (topic_type.equals("3")) {
				 sql = "select DISTINCT t1.content as name, (((SELECT count(*) FROM `t_txby_bd_sysAnswer` where optionId = #{pojo.optionId})*100)/ (SELECT count(*) FROM `t_txby_bd_sysAnswer`"
				 		+ " where topicId = "+topic_type+")) as value from t_txby_bd_sysAnswer t1,t_txby_bd_sysOption t2 where t1.optionId = t2.id_ and t1.optionId = #{pojo.optionId}";
			}*/
			 sql = "SELECT type, COUNT(DISTINCT userId) AS Mumbercount , COUNT(DISTINCT userId) / "+mapi.get("total")+" AS percent FROM t_txby_bd_sysAnswer GROUP BY type";
		//System.out.println(JSON.toJSON(list));
		PageList<Map> pl = this.mBaseDao.findBySqlForPageList(sql, Map.class, Query.getPageBean(params), null);
		PageUtils pageUtil = new PageUtils(pl.getResultList(), pl.getPageBean().getTotalCount().intValue(),  pl.getPageBean().getPageSize(), pl.getPageBean().getPage());
		return R.ok().put("page", pageUtil);
	}
	/**
	 * 查询题干
	 */
	public List queryOptionIdList(){
		String sql="select id_ from t_txby_sickDetailInfo";
		List queryOptionIdList=(List) this.mBaseDao.findBySql(sql, Map.class, null);
		return queryOptionIdList;
	}
	
	/**
	 * 查询患者总人数
	 */
	public Map queryList(){
		String sql="select count(1) as total from t_txby_sickDetailInfo";
		Map queryList=(Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
		return queryList;
	}
	
		
}
